/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spielo.crystaldesigner.gui;

import com.spielo.crystaldesigner.dao.*;
import com.spielo.crystaldesigner.render.StyleRenderer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author wreiterer
 */
public class CrystalMainWindow extends javax.swing.JFrame {

    private ExtractArchive extractArchive = new ExtractArchive();
    private LanguageReader languageReader = new LanguageReader();
    private ScreenReader screenReader = new ScreenReader();
    private StyleReader styleReader = new StyleReader();
    private StyleRenderer styleRenderer = new StyleRenderer();
    private LanguageChanger languageChanger = new LanguageChanger();
    
    private boolean displayScreens = true;
    private DefaultComboBoxModel<String> screenStyleComboModel;
    private DefaultListModel<String> screenStyleListModel;

    /**
     * Creates new form CrystalMainWindow
     */
    public CrystalMainWindow() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.initializeLanguages();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        screenStyleCombo = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        screenStyleList = new javax.swing.JList();
        btSwitch = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        onOpen = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        btChangeBackground = new javax.swing.JMenuItem();
        languageMenu = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel3.setMinimumSize(new java.awt.Dimension(150, 200));
        jPanel3.setPreferredSize(new java.awt.Dimension(150, 277));
        jPanel3.setLayout(new java.awt.BorderLayout());

        screenStyleCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onChangeListContent(evt);
            }
        });
        jPanel3.add(screenStyleCombo, java.awt.BorderLayout.PAGE_START);

        screenStyleList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                onShowPreview(evt);
            }
        });
        jScrollPane3.setViewportView(screenStyleList);

        jPanel3.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        btSwitch.setText("Switch to Styles");
        btSwitch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSwitch(evt);
            }
        });
        jPanel3.add(btSwitch, java.awt.BorderLayout.PAGE_END);

        jSplitPane1.setLeftComponent(jPanel3);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 321, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel4);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        onOpen.setText("Open Package");
        onOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOpen(evt);
            }
        });
        jMenu1.add(onOpen);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        btChangeBackground.setText("Change Background");
        btChangeBackground.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onChangeBackground(evt);
            }
        });
        jMenu2.add(btChangeBackground);

        languageMenu.setText("Change Package Language");
        jMenu2.add(languageMenu);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Other");

        jMenuItem1.setText("Show Packageinfo");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onShowPackageInfo(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onOpen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOpen
        JFileChooser fc = new JFileChooser();
        int returnval;
        File dpkg = null;


        fc.setDialogTitle("Designerpackage Auswahl");
        fc.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".dpkg") || f.isDirectory();
            }

            @Override
            public String getDescription() {
                return "Designer Packages(*.dpkg)";
            }
        });
        fc.showOpenDialog(this);
        dpkg = fc.getSelectedFile();

        if (dpkg != null) {
            extractArchive.extractArchive(dpkg);

            screenStyleComboModel = new DefaultComboBoxModel<>();
            screenStyleListModel = new DefaultListModel<>();
            for (String s : screenReader.getSubfolderList()) {
                screenStyleComboModel.addElement(s);
            }
            for (String screen : screenReader.getScreenList("")) {
                screenStyleListModel.addElement(screen);
            }

            screenStyleCombo.setModel(screenStyleComboModel);
            screenStyleList.setModel(screenStyleListModel);

            screenStyleCombo.updateUI();
            screenStyleList.updateUI();
        } else {
            return;
        }


    }//GEN-LAST:event_onOpen

    private void onChangeBackground(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onChangeBackground
        CurrentBackgroundDialog bgDialog = new CurrentBackgroundDialog(null, true);
        bgDialog.setVisible(rootPaneCheckingEnabled);


    }//GEN-LAST:event_onChangeBackground

    private void onShowPackageInfo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onShowPackageInfo
        PropertyDialog pd = new PropertyDialog(this, true);
        pd.setVisible(true);
    }//GEN-LAST:event_onShowPackageInfo

    private void onChangeListContent(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onChangeListContent
        String subfolder = (String) screenStyleComboModel.getSelectedItem();
        if (subfolder != null) {
            if (displayScreens) {
                screenStyleListModel.removeAllElements();
                for (String screen : screenReader.getScreenList(subfolder)) {
                    screenStyleListModel.addElement(screen);
                }
            } else {
                screenStyleListModel.removeAllElements();
                for (String style : styleReader.getStyleList(subfolder)) {
                    System.out.println(style);
                    screenStyleListModel.addElement(style);
                }
            }
        }
    }//GEN-LAST:event_onChangeListContent

    private void onSwitch(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSwitch

        screenStyleComboModel.removeAllElements();
        screenStyleListModel.removeAllElements();
        if (displayScreens) {
            displayScreens = false;
            btSwitch.setText("Switch to Screens");

            //write styles into combobox and list
            System.out.println(screenStyleListModel.getSize());
            for (String s : styleReader.getSubfolderList()) {

                screenStyleComboModel.addElement(s);
 
            }
     

        } else {
            displayScreens = true;
            btSwitch.setText("Switch to Styles");

            //write screens into combobox and list
            for (String s : screenReader.getSubfolderList()) {
                screenStyleComboModel.addElement(s);
            }


        }

    }//GEN-LAST:event_onSwitch


    private void initializeLanguages()
    {
        ArrayList<String> languages = languageReader.getLanguages();
        ActionListener changeLanguage = new ChangeLanguage();
        if(languages.size() > 0)
        {
            for(int i=0; i<languages.size(); i++)
            {
                JMenuItem item = new JMenuItem(languages.get(i));
                item.addActionListener(changeLanguage);
                languageMenu.add(item);
            }
        }
    }    
    private class ChangeLanguage implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            JMenuItem item = (JMenuItem) ae.getSource();
            languageChanger.change(item.getText());
        }
    }
    
    

    private void onShowPreview(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onShowPreview
        styleRenderer.render();
    }//GEN-LAST:event_onShowPreview


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CrystalMainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CrystalMainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CrystalMainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CrystalMainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CrystalMainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem btChangeBackground;
    private javax.swing.JButton btSwitch;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JMenu languageMenu;
    private javax.swing.JMenuItem onOpen;
    private javax.swing.JComboBox screenStyleCombo;
    private javax.swing.JList screenStyleList;
    // End of variables declaration//GEN-END:variables


}
